﻿using System;
using System.IO;
using System.Web;
using jSLCore.Service.Workflow;

namespace jSLApp.Web
{
    public class ImageHandler : IHttpHandler
    {
        private const string DirectoryName = "Htmls";

        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            var fileId = Path.GetFileNameWithoutExtension(context.Request.FilePath);
            // ReSharper disable AssignNullToNotNullAttribute
            var filePath = Path.Combine(Path.Combine(ServerHelper.CurrentWebApplicationPhysycalPath, DirectoryName), fileId);
            filePath = filePath + ".jpg";
            // ReSharper restore AssignNullToNotNullAttribute
            context.Response.BinaryWrite(
                File.ReadAllBytes(filePath));
        }

        #endregion
    }
}
